package org.chenmin.gwt.client.components;

import com.axellience.vuegwt.core.annotations.component.Component;
import com.axellience.vuegwt.core.annotations.component.Data;
import com.axellience.vuegwt.core.client.component.IsVueComponent;
import com.axellience.vuegwt.core.client.component.hooks.HasCreated;
import com.axellience.vueroutergwt.client.Route;
import com.axellience.vueroutergwt.client.VueRouteComponent;
import com.axellience.vueroutergwt.client.VueRouter;
//import com.google.gwt.user.client.Window;
import com.google.gwt.core.client.GWT;
import io.reinert.gdeferred.DoneCallback;
import io.reinert.requestor.Requestor;
import jsinterop.annotations.JsMethod;
import org.chenmin.gwt.client.js.Materialize;

import java.util.logging.Logger;

@Component
public class NavsComponent implements IsVueComponent,VueRouteComponent,HasCreated {
    Logger logger = Logger.getLogger(NavsComponent.class.getName());
    @Data
    Route route;
    @Data
    VueRouter router;


    Requestor r = GWT.create(Requestor.class);

    @Override
    public void created() {
        route =  getRoute();
        router = getRouter();
        logger.info("route:"+route);
        logger.info("router:"+router);
    }
    public void pushss(){
//        router = getRouter();
//        router.push(r);
        ///bar/4
//        Location location = new Location();
//        location.setPath("/no");
//        router.push(location);
        say("/bar/212");
        router.push("/bar/212");
    }

    @JsMethod
    public void say(String message) {
//        Window.alert(message);
//        Materialize materialize = new Materialize();
        Materialize.toast("1ss",1000);
        Materialize.toast(message,1000);
        logger.info("begin ip");
        logger.info(r.toString());
        r.req("http://httpbin.org/ip")
                .contentType("application/json")
                // .header("Content-Type", "application/json; charset=utf-8")
                //.header("Content-Type", "application/json")
                .get(Ip.class).done(new DoneCallback<Ip>() {
            public void onDone(Ip ip) {
                Materialize.toast(ip.getOrigin(),4000);
            }
        });
//        RequestPromise<Ip> promise = r.req("http://httpbin.org/ip").get(Ip.class);
//        Materialize.toast(promise.,4000);
    }

}
